<template>
  <div class="project-overview">
    <el-card class="overview-card">
      <template #header>
        <div class="card-header">
          <h3>📊 项目概览</h3>
        </div>
      </template>

      <div v-if="loading" class="loading-container">
        <el-skeleton :rows="3" animated />
      </div>

      <div v-else-if="overview" class="overview-content">
        <div class="overview-grid">
          <div class="overview-item">
            <div class="item-icon">🎯</div>
            <div class="item-content">
              <div class="item-label">项目名称</div>
              <div class="item-value">{{ overview.name }}</div>
            </div>
          </div>

          <div class="overview-item">
            <div class="item-icon">📁</div>
            <div class="item-content">
              <div class="item-label">项目类型</div>
              <div class="item-value">{{ overview.type }}</div>
            </div>
          </div>

          <div class="overview-item">
            <div class="item-icon">📦</div>
            <div class="item-content">
              <div class="item-label">包数量</div>
              <div class="item-value">{{ overview.packages }}</div>
            </div>
          </div>

          <div class="overview-item">
            <div class="item-icon">📄</div>
            <div class="item-content">
              <div class="item-label">文件数量</div>
              <div class="item-value">{{ overview.files }}</div>
            </div>
          </div>

          <div class="overview-item">
            <div class="item-icon">🔗</div>
            <div class="item-content">
              <div class="item-label">依赖数量</div>
              <div class="item-value">{{ overview.dependencies }}</div>
            </div>
          </div>

          <div class="overview-item">
            <div class="item-icon">⏰</div>
            <div class="item-content">
              <div class="item-label">分析时间</div>
              <div class="item-value">{{ overview.analysisTime }}</div>
            </div>
          </div>
        </div>
      </div>

      <div v-else class="error-container">
        <el-alert
          title="加载失败"
          description="无法加载项目概览数据"
          type="error"
          show-icon
        />
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, type Ref } from 'vue';
import { AnalysisDataService } from '../services/analysis-data';

/**
 * 项目概览数据接口
 */
interface IProjectOverview {
  name: string;
  type: string;
  packages: number;
  files: number;
  dependencies: number;
  analysisTime: string;
}

// 响应式数据
const loading: Ref<boolean> = ref(true);
const overview: Ref<IProjectOverview | null> = ref(null);
const analysisService = AnalysisDataService.getInstance();

/**
 * 加载项目概览数据
 */
const loadOverviewData = async (): Promise<void> => {
  try {
    loading.value = true;

    // 检查是否已有数据
    let currentData = analysisService.getCurrentAnalysisData();

    if (!currentData) {
      // 加载数据（路径从环境变量获取）
      await analysisService.loadAnalysisData();
      currentData = analysisService.getCurrentAnalysisData();
    }

    if (currentData) {
      overview.value = analysisService.getProjectOverview();
    }
  } catch (error) {
    console.error('加载项目概览数据失败:', error);
  } finally {
    loading.value = false;
  }
};

// 组件挂载时加载数据
onMounted(() => {
  loadOverviewData();
});
</script>

<style scoped>
.project-overview {
  margin-bottom: 20px;
}

.overview-card {
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.card-header h3 {
  margin: 0;
  color: #303133;
  font-size: 18px;
  font-weight: 600;
}

.loading-container {
  padding: 20px;
}

.overview-content {
  padding: 10px 0;
}

.overview-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 16px;
  margin: 0;
}

.overview-item {
  display: flex;
  align-items: center;
  padding: 12px 16px;
  background: #f8f9fa;
  border-radius: 6px;
  transition: all 0.3s ease;
}

.overview-item:hover {
  background: #e9ecef;
  transform: translateY(-1px);
}

.item-icon {
  font-size: 24px;
  margin-right: 12px;
  min-width: 32px;
  text-align: center;
}

.item-content {
  flex: 1;
}

.item-label {
  font-size: 12px;
  color: #909399;
  margin-bottom: 4px;
  font-weight: 500;
}

.item-value {
  font-size: 16px;
  color: #303133;
  font-weight: 600;
}

.error-container {
  padding: 20px;
}
</style>
